<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
       <el-form-item label="组卷名称" prop="paperId">
            <el-select v-model="queryParams.paperId"  placeholder="请选择组卷名称">
            <el-option
              v-for="item in paperOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      <el-form-item label="题库标识" prop="questionStoreId">
        <el-input
          v-model="queryParams.questionStoreId"
          placeholder="请输入题库标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计分" prop="score">
        <el-input
          v-model="queryParams.score"
          placeholder="请输入计分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:question:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:question:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:question:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:question:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="questionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column v-if="false" label="标识" align="center" prop="id" />
      <el-table-column label="组卷标识" align="center" prop="paperId" />
      <el-table-column label="题库标识" align="center" prop="questionStoreId" />
      <el-table-column label="计分" align="center" prop="score" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:question:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:question:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改组卷题对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
         <el-form-item label="组卷名称" prop="paperId">
            <el-select v-model="form.paperId"  placeholder="请选择组卷名称">
            <el-option
              v-for="item in paperOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
       <el-form :model="queryQuestionStoreParams" ref="queryQuestionStoreForm" :inline="true">
      <el-form-item label="题型" prop="typeCode">
        <el-input
          v-model="queryQuestionStoreParams.typeCode"
          placeholder="请输入题型"
          clearable
          size="small"
          @keyup.enter.native="handleQueryQuestionStore"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryQuestionStore">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQueryQuestionStore">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table v-loading="loading1" ref="list"  :data="questionStoreList"  height="260px">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="标识" v-if="false" align="center" prop="id" />
        <el-table-column prop="typeName" label="题型"></el-table-column>
        <el-table-column prop="content" label="题面"></el-table-column>
        <el-table-column prop="score" label="计分" >
        <template slot-scope="scope">
          <el-input v-model="scope.row.score" ></el-input>
        </template>
      </el-table-column>
      </el-table>
      <pagination
        v-show="questionStoreTotal>0"
        :total="questionStoreTotal"
        :page.sync="queryQuestionStoreParams.pageNum"
        :limit.sync="queryQuestionStoreParams.pageSize"
        @pagination="getListPaper"
      />
    </el-row>
       
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getquestionStore,listquestionStore,listPaper,listQuestion, getQuestion, delQuestion, addQuestion, updateQuestion, exportQuestion } from "@/api/system/question";

export default {
  name: "Question",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 习题遮罩层
      loading1: false,
      // 选中数组
      ids: [],
      //习题选中数组
      list:[],
      //组卷数据
      paperOptions:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
       //习题表格数据
      questionStoreList: [],
      // 总条数
      questionStoreTotal: 0,
      // 组卷题表格数据
      questionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paperId: null,
        questionStoreId: null,
        score: null
      },
      queryQuestionStoreParams:{
        pageNum: 1,
        pageSize: 10,
        typeCode: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getListPaper();
  },
  methods: {
    //查询组卷
     getListPaper() {
       listPaper(this.queryParams).then(response => {
        this.paperOptions = response.rows;
      });
    },
    /** 查询组卷题列表 */
    getList() {
      this.loading = true;
      listQuestion(this.queryParams).then(response => {
        this.questionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        paperId: null,
        questionStoreId: null,
        score: null,
        list:[]
      };
      this.resetForm("form");
      this.questionStoreList = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //搜索习题
    handleQueryQuestionStore(){
       this.queryQuestionStoreParams.pageNum = 1;
       this.getQuestionStoreList();
    },
    getQuestionStoreList() {
      this.loading1 = true;
      listquestionStore(this.queryQuestionStoreParams).then(response => {
        this.questionStoreList = response.rows;
        this.questionStoreTotal = response.total;
        this.loading1 = false;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    resetQueryQuestionStore(){
      this.resetForm("queryQuestionStoreForm");
      this.handleQueryQuestionStore();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加组卷题";
      this.paperOptions = [];
      this.queryParams.pageSize = 999;
      this.getListPaper();
      this.handleQueryQuestionStore();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
       this.getListPaper();
      const id = row.id || this.ids
      getQuestion(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改组卷题";
      });
      getquestionStore(row.questionStoreId).then(response => {
        response.data.score = row.score;
        this.questionStoreList.push(response.data);
        this.questionStoreTotal = 1;
        this.$refs.list.toggleAllSelection();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
              var resp = this.$refs.list.selection;
             var listMap = [];
            resp.forEach(item => {
              var list = {};
              list.questionStoreId = item.id;
              list.score = item.score;
              listMap.push(list);
            });
            this.form.list = listMap;     
          if (this.form.id != null) {
            updateQuestion(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addQuestion(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除组卷题编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delQuestion(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有组卷题数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportQuestion(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
